package cn.com.dhc.danlu.activity.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.CacheWebView;
import cn.com.dhc.danlu.widget.CacheWebView.OnWebViewClient;

/**
 * @author wuhaiyang
 */
public class WebViewActivity extends DanluBaseActivity {
    private CacheWebView mWebView;
    private ProgressBar pbLoading;
    private LinearLayout llError;
    private LinearLayout llCallHelp;
    private RelativeLayout rlContent;

    private String url;
    private String title;
    private boolean isNeedHelp;

    /**
     * 跳转至WebViewActivity
     *
     * @param context
     *         发起跳转的Activity 上下文
     * @param title
     *         标题
     * @param url
     *         WebView 加载链接
     */
    public static void startSelf(Context context, String title, String url, boolean isNeedHelp) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        intent.putExtra("isNeedHelp", isNeedHelp);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dl_activity_webview);

        initArgs();
        initView();
        setWebView();
        setViews();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (mWebView != null) {
                mWebView.removeAllViews();
                mWebView.destroy();
                mWebView = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initArgs() {
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        isNeedHelp = getIntent().getBooleanExtra("isNeedHelp", false);
    }

    private void initView() {
        mWebView = (CacheWebView) findViewById(R.id.cw_web);
        llError = (LinearLayout) findViewById(R.id.ll_error);
        pbLoading = (ProgressBar) findViewById(R.id.pb_loading);
        rlContent = (RelativeLayout) findViewById(R.id.rl_content);
        llCallHelp = (LinearLayout) findViewById(R.id.ll_call_help);
    }

    private void setWebView() {
        mWebView.setCustomeWebViewClient(new OnWebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                showErrorPage();
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                pbLoading.setProgress(newProgress);
                if (newProgress == 100) {
                    // 没有title显示网址
                    pbLoading.setVisibility(View.INVISIBLE);
                    AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
                    animation.setDuration(500);
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            pbLoading.setVisibility(View.INVISIBLE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                    pbLoading.startAnimation(animation);
                } else {
                    pbLoading.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
    }

    private void setViews() {
        if (!TextUtils.isEmpty(title)) {
            mToolbar.setTitle(title);
        }

        if (isNeedHelp) {
            llCallHelp.setVisibility(View.VISIBLE);
            llCallHelp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommonUtils.callHelp(WebViewActivity.this);
                }
            });
        }

        llError.findViewById(R.id.btn_error_retry).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llError.setVisibility(View.GONE);
                mWebView.reload();
                rlContent.setVisibility(View.VISIBLE);
            }
        });

        mWebView.loadUrl(url);
    }

    protected void showErrorPage() {
        rlContent.setVisibility(View.GONE);
        llError.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}